package com.d404.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.server.DownloadInfo;
import im.beans.Classify;
import im.beans.User;
import manager.PersistentManager;

/**
 * 删除分组
 * Servlet implementation class DeleteClass
 */
//@WebServlet("/DeleteClass")
public class DeleteClass extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteClass() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		Upload upload = null;
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try
		{
			String data = request.getParameter("data");
			upload = JSON.parseObject(data,Upload.class);
			
			User user = new User();
			user.setId(Long.parseLong(upload.getUserId()));
			
			Classify classify = new Classify();
			classify.setId(Long.parseLong(upload.getClassId()));
			
			// 获取默认分组
			Query qClassify = session.createQuery("from Classify csf where csf.classifyName = ? and csf.ower = ?");
			qClassify.setParameter(0, "未分组");
			qClassify.setParameter(1, user);
			qClassify.setMaxResults(1);
			
			Classify defaultClassify = (Classify) qClassify.list().get(0);
			
			// 更新分组到默认分组
			Query updateClassify = session.createQuery("update from Classify_User cu set cu.classify = ? where cu.classify = ?");
			updateClassify.setParameter(0, defaultClassify);
			updateClassify.setParameter(1, classify);
			updateClassify.executeUpdate();
			
			// 删除分组
//			Query deleteClassify = session.createQuery("delete from Classify csf where csf.id=?");
//			deleteClassify.setParameter(0, classify);
			
			session.delete(classify);
			
			tx.commit();
			download.setStatus(DownloadInfo.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		}finally {
//			session.close();
			response.getWriter().append(JSON.toJSONString(download));
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	// 上行数据
	public static class Upload
	{
		// 分组id
		String classId;
		
		// 用户id
		String userId;

		public String getClassId() {
			return classId;
		}

		public void setClassId(String classId) {
			this.classId = classId;
		}

		public String getUserId() {
			return userId;
		}

		public void setUserId(String userId) {
			this.userId = userId;
		}
		
		
		
	}
	
	// 下行数据
	public static class Download
	{
		int status;

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}
		
		
	}
}
